/****************************************
Figure 3, Table A.4
*****************************************/

* Load Data
use "$dir/Data/Final/Isonymy_sy.dta", clear 

rename (state statename) (stateabb state)
  	  
* Merge with year of ban data
merge m:1 state using "Data/Original/year_of_ban.dta"
keep if _merge == 3
drop _merge

gen cm_rate = 4*max(0, IsoNonrand_sy)
encode state, gen(stateid)


gen i = stateid
gen t = decade/10 
gen Ei = round(year_of_ban/10) 	// decade when unit is first treated

gen K = t-Ei 			// "relative time" num periods since treated (missing if never-treated)

sort i t


forvalues l = 1/6 {
	gen A`l'event = K==-`l'
}

forvalues l = 0/6 {
	gen P`l'event = K==`l'
}


gen insample6treat = abs(K) <= 6 & K != .


* Label Variable
label var cm_rate "Cousin marriage rate"


* Interaction treatment vars
forvalues pre = 6(-1)2 {
	cap n gen cmH_Pre`pre' = cmH * A`pre'event
}

forvalues post = 0/6 {
	cap n gen cmH_Post`post' = cmH * P`post'event
}

forvalues x = 0/6 {
	cap label var cmH_Pre`x' "Pre `x'"
	cap label var cmH_Post`x' "Post `x'"
}



replace Ei = . if cmH == 0 /* Treating low CM surnames as never having faced a ban */
gen nevertreated = Ei == . /* this includes low CM surnames and those that never faced a ban */

drop if year_of_ban > 1940 /* this drops all states that banned after 1940 or never banned because they
have year of ban value missing, so control group is low cousin marriage surnames in states that banned
by 1940 */


keep if insample6treat == 1


compress	

* Dropping state-year cells with less than 50 obs
drop if N_sy < 50

/**************************************
Figure 3
**************************************/

eventstudyinteract cm_rate  ///
	cmH ///
    cmH_P* ///
    , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	su cm_rate if e(sample) == 1
	local omean = string(round(r(mean), .01))
	
event_plot e(b_iw)#e(V_iw), default_look ///
	stub_lag(cmH_Post#) stub_lead(cmH_Pre#) ///
	graph_opt(ytitle("Cousin marriage rate") xtitle("Decades since ban") ///
	title("") xlabel(-6(1)6) ylabel(-0.08(0.02)0.08) legend(off) )
	
graph export "Output/TablesFigures/Fig_FS.png", replace
	
/**************************************
Table A.4
**************************************/

eststo clear
eventstudyinteract cm_rate  ///
	cmH ///
    cmH_P* ///
    , absorb(i.i##i.t i.year i.i##i.cmH) vce(cluster i) ///
	cohort(Ei) control_cohort(nevertreated)
	
 	matrix b_iw = e(b_iw) // Save the results for table output
 	matrix V_iw = e(V_iw)
	capture matrix V_iw = diag(e(V_iw))

 	erepost b = b_iw V = V_iw 
 	esttab
	eststo A1
	local obs1 = r(N)
    su cm_rate if e(sample) == 1
    local meanA = string(round(r(mean), .01))
 
 estout A1 using "Output/TablesFigures/results_fs.tex", style(tex) replace ///
    keep(cmH_*) ///
	cells(b(star fmt(%9.4f)) se(par)) ///
	nolabel collabels(none) mlabels(none) starlevels(* 0.10 ** 0.05 *** 0.01) ///
	varlabels(cmH_Pre6 "-6" cmH_Pre5 "-5" ///
	cmH_Pre4 "-4" cmH_Pre3 "-3" ///
	cmH_Pre2 "-2" cmH_Post0 "0" ///
	cmH_Post1 "1" cmH_Post2 "2" ///
	cmH_Post3 "3" cmH_Post4 "4" ///
	cmH_Post5 "5" cmH_Post6 "6")

local tex " \\ \hline"
local tex "`tex' Observations & `obs1' \\"
local tex "`tex' Mean Dep. Var & `meanA'   \\"
local tex "`tex' State X Decade FE & Yes \\"
local tex "`tex' State X High CM F.E. & Yes \\"
local tex "`tex' Census year F.E. & Yes  \\"
local tex "`tex' \multicolumn{3}{p{8cm}}{\tiny \textit{Notes:} Standard errors clustered at state-level."
local tex "`tex' *** p<0.01, ** p<0.05, * p<0.1.} \\ \end{tabular} }"
	
esttab A1 using "Output/TablesFigures/results_fsa.tex", style(tex) replace booktabs ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("(1)") ///
	mgroups("Cousin marriage rate", pattern(1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	